package com.platform.service.impl;

import com.platform.Global;
import com.platform.dao.SysMerchantDao;
import com.platform.entity.SysMerchantEntity;
import com.platform.service.SysMerchantService;
import com.platform.service.SysRoleService;
import com.platform.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户Service实现类
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-24 11:30:54
 */
@Service("sysMerchantService")
public class SysMerchantServiceImpl implements SysMerchantService {
    @Autowired
    private SysMerchantDao sysMerchantDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public SysMerchantEntity queryObject(Long userId) {
        return sysMerchantDao.queryObject(userId);
    }

    @Override
    public List<SysMerchantEntity> queryList(Map<String, Object> map) {
        return sysMerchantDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysMerchantDao.queryTotal(map);
    }

    @Override
    public int save(SysMerchantEntity sysMerchant) {
        sysMerchant.setCreateDate(new Date());
        //sha256加密
        if(sysMerchant.getPassword()!=null&&sysMerchant.getPassword().length()>0)
            sysMerchant.setPassword(new Sha256Hash(sysMerchant.getPassword()).toHex());
        else
            sysMerchant.setPassword(new Sha256Hash(Global.DEFAULT_PASS_WORD).toHex());
        int save = sysMerchantDao.save(sysMerchant);
        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(sysMerchant.getUserId(), sysMerchant.getRoleIdList());
        return save;
    }

    @Override
    public int updatePassword(Long userId, String password, String newPassword) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("password", password);
        map.put("newPassword", newPassword);
        return sysMerchantDao.updatePassword(map);
    }

    @Override
    public int update(SysMerchantEntity sysMerchant) {

        if (StringUtils.isBlank(sysMerchant.getPassword())) {
            sysMerchant.setPassword(new Sha256Hash(Global.DEFAULT_PASS_WORD).toHex());
        } else {
            sysMerchant.setPassword(new Sha256Hash(sysMerchant.getPassword()).toHex());
        }
        int update = sysMerchantDao.update(sysMerchant);


        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(sysMerchant.getUserId(), sysMerchant.getRoleIdList());

        return update;
    }

    @Override
    public int delete(Long userId) {
        return sysMerchantDao.delete(userId);
    }

    @Override
    public int deleteBatch(Long[] userIds) {
        return sysMerchantDao.deleteBatch(userIds);
    }

    @Override
    public List<SysMerchantEntity> querySysmerchant() {
        return sysMerchantDao.querySysmerchant();
    }
}
